package com.neuronbit.businessflow.intellij.model;

import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * The type Return.
 */
@Getter
public class Return implements Serializable {
    @Setter
    private String name;
    @Setter
    private String description;
    private Set<ParamValue> value = new HashSet<>(0);
    @Setter
    private Object defaultValue;
    /**
     * 这个字段不应该被序列化，否则需要维持其一致性
     */
    @Setter
    private String type;

    /**
     * Instantiates a new Return.
     */
    public Return() {
    }

    /**
     * Instantiates a new Return.
     *
     * @param name  the name
     * @param value the value
     */
    public Return(String name, String value) {
        this.name = name;
        setValue(value);
    }

    /**
     * Sets value.
     *
     * @param value the value
     */
    public void setValue(String value) {
        this.value.clear();
        if (value.contains(",")) {
            String[] split = value.split(",");

            for (String s : split) {
                if (StringUtils.isNoneBlank(s)) {
                    this.value.add(new ParamValue(s));
                }
            }
        } else {
            this.value.add(new ParamValue(value));
        }
    }

    /**
     * Sets value.
     *
     * @param value the value
     */
    public void setValue(Set<ParamValue> value) {
        if (null == value) {
            return;
        }
        this.value.clear();
        this.value.addAll(value);
    }

    /**
     * Gets value.
     *
     * @return the value
     */
    public String getValue() {
        if (null != value) {
            return value.stream()
                    .filter(Objects::nonNull)
                    .map(ParamValue::toString)
                    .filter(StringUtils::isNotBlank)
                    .collect(Collectors.joining(","));
        } else {
            return "";
        }
    }

    /**
     * Get field names string [ ].
     *
     * @return the string [ ]
     */
    public String[] getFieldNames() {
        if (null == name || !name.startsWith("return.output")) {
            return new String[0];
        } else {
            String[] split = name.split("\\.");
            return ArrayUtils.subarray(split, 1, split.length);
        }
    }

    /**
     * Gets values.
     *
     * @return the values
     */
    public Set<ParamValue> getValues() {
        return value;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Return aReturn = (Return) o;
        return Objects.equals(name, aReturn.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name);
    }

    /**
     * Sets value.
     *
     * @param paramValue the param value
     */
    public void setValue(ParamValue paramValue) {
        if (null != paramValue) {
            value.add(paramValue);
        }
    }
}
